package com.dynamictpm.toolkit.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.*;

/**
 * 描述：： 阻塞队列枚举
 * <p>
 * ------------------------------------
 *
 * @program: dynamic-tpm
 * @ClassName QueueTypeEnum
 * @author: it-yml
 * @create: 2023-07-05 10:02
 * @Version 1.0
 * --------------------------------------
 **/
@Slf4j
public enum QueueTypeEnum {

    /**
     * {@link java.util.concurrent.ArrayBlockingQueue}
     */
    ARRAY_BLOCKING_QUEUE(1),

    /**
     * {@link java.util.concurrent.LinkedBlockingQueue}
     */
    LINKED_BLOCKING_QUEUE(2),

    /**
     * {@link java.util.concurrent.LinkedBlockingDeque}
     */
    LINKED_BLOCKING_DEQUE(3),

    /**
     * {@link java.util.concurrent.SynchronousQueue}
     */
    SYNCHRONOUS_QUEUE(4),

    /**
     * {@link java.util.concurrent.LinkedTransferQueue}
     */
    LINKED_TRANSFER_QUEUE(5),

    /**
     * {@link java.util.concurrent.PriorityBlockingQueue}
     */
    PRIORITY_BLOCKING_QUEUE(6),

    /**
     * {@link "io.dynamic.threadpool.starter.toolkit.thread.ResizableCapacityLinkedBlockIngQueue"}
     */
    RESIZABLE_LINKED_BLOCKING_QUEUE(9);

    public Integer type;

    QueueTypeEnum(int type) {
        this.type = type;
    }

    static {
        log.info("加载spi 自定义阻塞队列异常");
//        DynamicTpServiceLoader.register(CustomBlockingQueue.class);
    }

    public static BlockingQueue createBlockingQueue(Integer type, Integer capacity) {
        BlockingQueue blockingQueue = null;
        if (Objects.equals(type, ARRAY_BLOCKING_QUEUE.type)) {
            blockingQueue = new ArrayBlockingQueue(capacity);
        } else if (Objects.equals(type, LINKED_BLOCKING_QUEUE.type)) {
            blockingQueue = new LinkedBlockingQueue(capacity);
        } else if (Objects.equals(type, LINKED_BLOCKING_DEQUE.type)) {
            blockingQueue = new LinkedBlockingDeque(capacity);
        } else if (Objects.equals(type, SYNCHRONOUS_QUEUE.type)) {
            blockingQueue = new SynchronousQueue();
        } else if (Objects.equals(type, LINKED_TRANSFER_QUEUE.type)) {
            blockingQueue = new LinkedTransferQueue();
        } else if (Objects.equals(type, PRIORITY_BLOCKING_QUEUE.type)) {
            blockingQueue = new PriorityBlockingQueue(capacity);
        }
        // 自定义阻塞队列
//        else if (Objects.equals(type, RESIZABLE_LINKED_BLOCKING_QUEUE.type)) {
//            blockingQueue = new ResizableCapacityLinkedBlockIngQueue(capacity);
//        }
//
//        Collection<CustomBlockingQueue> customBlockingQueues = DynamicTpServiceLoader
//                .getSingletonServiceInstances(CustomBlockingQueue.class);
//        blockingQueue = Optional.ofNullable(blockingQueue).orElseGet(() -> customBlockingQueues.stream()
//                .filter(each -> Objects.equals(type, each.getType()))
//                .map(each -> each.generateBlockingQueue())
//                .findFirst()
//                .orElse(new LinkedBlockingQueue(capacity)));

        return blockingQueue;
    }
}
